﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;

namespace School.Pages.BOOKS
{
    public partial class SRV_BOOKS_PRICING : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1106) == true)
                {
                    DropDownList ddl = ((DropDownList)(CampusPackage1.FindControl("ddlGrade")));
                    ddl.AutoPostBack = true;
                    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
                    lblResult.Text = "";
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList Gradeddl = (DropDownList)sender;
            if (Gradeddl.SelectedIndex != 0)
            {
                if (CampusPackage1.CampusId != "0" && CampusPackage1.CertificateId != "0" && CampusPackage1.GradeId != "0" && CampusPackage1.StageId != "0")
                {
                    gvData.DataSource = School.Service.BOOKS.SRV_BOOK_Price.BookPrice(long.Parse(CampusPackage1.CampusId),
                        long.Parse(CampusPackage1.CertificateId),long.Parse(CampusPackage1.GradeId),long.Parse(CampusPackage1.StageId));
                    gvData.DataBind();
                }
            }
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            List<SRV_Book_Price> lstPrices = new List<SRV_Book_Price>();
            TextBox txbprice;
            if (gvData.Rows.Count > 0)
                foreach (GridViewRow item in gvData.Rows)
                {
                    if (item.RowType == DataControlRowType.DataRow)
                    {
                        txbprice = (TextBox)item.Cells[3].FindControl("txtPrice");
                        lstPrices.Add(new SRV_Book_Price()
                        {
                            //Price_Id = long.Parse(GridView1.DataKeys[item.RowIndex].Values["Id"].ToString()),
                            Book_Id = int.Parse(gvData.DataKeys[item.RowIndex].Values["book_Id"].ToString()),
                            Price = double.Parse(txbprice.Text),
                            DateTime = DateTime.Now,

                        });
                    }
                }
            if (School.Service.BOOKS.SRV_BOOK_Price.PriceInsert(lstPrices))
            {
                lblResult.Text = "تمت عمليه الاضافه بنجاح";
                ClearGrid();

            }
        }
        public void ClearGrid()
        {
            foreach (GridViewRow item in gvData.Rows)
            {
                if (item.RowType == DataControlRowType.DataRow)
                {
                    TextBox txbprice = (TextBox)item.Cells[3].FindControl("txtPrice");
                    txbprice.Text = "";
                }
            }

        }
    }
}